package com.gsx.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsx.entity.Type;
import com.gsx.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admain")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    public String pages(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                        @RequestParam(name="size",required = true,defaultValue = "5")int size,Model model){

        List<Type> typeList=typeService.getAllType(page, size);
        PageInfo pageInfo=new PageInfo(typeList);
        model.addAttribute("pageInfo",pageInfo);
        return "admain/types";
    }

    @GetMapping("/types/input")
    public String input(Model model){
        model.addAttribute("tag",null);
        return "admain/type-input";
    }

    /*保存标签*/
    @PostMapping("/types")
    public String post(Type type, RedirectAttributes attributes){
        if(typeService.getTypeByName(type.getType_name())!=null){
            return "admain/type-input";
        }
        if(type.getType_name()==null||type.getType_name().equals("")){
            return "admain/type-input";
        }
        int result=typeService.saveType(type);
        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else{
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admain/types";
    }

    /*修改*/
    @GetMapping("types/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        model.addAttribute("type",typeService.getType(id));
        return "admain/type-input";
    }

    @PostMapping("/types/{id}")
    public String editPost(Type type, RedirectAttributes attributes,@PathVariable Long id){
        type.setType_id(id);
        if(typeService.getTypeByName(type.getType_name())!=null){
            return "admain/type-input";
        }
        if(type.getType_name()==null||type.getType_name().equals("")){
            return "admain/type-input";
        }
        int result=typeService.updateType(type);
        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else{
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admain/types";
    }

    /*删除类型*/
    @GetMapping("/types/{id}/delete")
    public String deleteType(@PathVariable Long id,RedirectAttributes attributes){
        int result=typeService.deleteType(id);
        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else{
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admain/types";
    }
}
